<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2023/5/23
 * Time: 11:28
 */

namespace app\admin\model;

use think\Db;
use think\Model;
class Orangs  extends Model
{
    /**
     * 获取毫秒
     */
    public static function getMillisecond() {
        list($s1, $s2) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    }
    /**
     * 直充下单接口
     */
        public static function send($mobile,$order_no,$product_id){
        $paramArr = [
            'timestamp'=>self::getMillisecond(),
            'amount'=>1,
            'app_id'=>19828270979,
//            'app_id'=>15020190001,
            'order_no'=>$order_no,
            'product_id'=>$product_id,
            'recharge_number'=>$mobile,
            'version'=>'1.0.0',
            'ip'=> $_SERVER['REMOTE_ADDR'],
            'oil_phone_account'=>$mobile,
            'notify_url'=>'https://hylh.kysxkj.cn/api/orange/canblak',
        ];
        $sign =self::createSign($paramArr);
        $strParam ='';
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $strParam .= $key.'='.urlencode($val).'&';
            }
        }
        $strParam .= 'sign='.$sign;
        $url = 'https://api.chengquan.cn/order/directCharge?'.$strParam;
//        $url ='http://test.api.chengquan.vip:11140/order/directCharge?'.$strParam;
        $result = file_get_contents($url);
        $result = json_decode($result,true);
        return $result;
    }

    public static function stock($goods_no){
        $paramArr = [
            'app_id'=>19828270979,
            'goods_no'=>$goods_no,
            'timestamp'=>self::getMillisecond(),
        ];
        $sign = self::createSign($paramArr);
        $strParam ='';
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $strParam .= $key.'='.urlencode($val).'&';
            }
        }
        $strParam .= 'sign='.$sign;
        $url = 'https://api.chengquan.cn/coupon/goods/stock?'.$strParam;
//        $url =  'http://test.api.chengquan.vip:11140/coupon/order/pay?'.$strParam;
        $result = file_get_contents($url);
        $result = json_decode($result,true);
        return $result;
    }

    /**
     * 卡密
     */
    public static function ksend($num,$goods_no){
        $paramArr = [
            'app_id'=>19828270979,
//            'app_id'=>15020190001,
            'user_order_no'=>self::makeOrderNum(),
            'goods_no'=>$goods_no,
            'count'=>$num,
            'mobile'=>'15182050531',
            'timestamp'=>self::getMillisecond(),
        ];
        $sign = self::createSign($paramArr);
        $strParam ='';
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $strParam .= $key.'='.urlencode($val).'&';
            }
        }
        $strParam .= 'sign='.$sign;
       $url = 'https://api.chengquan.cn/coupon/order/pay?'.$strParam;
//        $url =  'http://test.api.chengquan.vip:11140/coupon/order/pay?'.$strParam;
        $result = file_get_contents($url);
        $result = json_decode($result,true);
        return $result;
    }

    public static function createSign ($paramArr) {
        $strParam = '';
        ksort($paramArr);
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $strParam .= $key.'='.$val.'&';
            }
        }
        $strParam .= 'key='.'A29AF4A9A2F8FC02B138FF823E71AA3E';
//        $strParam .= 'key='.'0BE0C9FEE504983B285B9407CB9C85D9';
        $strParam =strtoupper(md5($strParam));
        return $strParam;
    }

    /**
     * 生成订单
     */
    public static function makeOrderNum()
    {
        @date_default_timezone_set("PRC");
        while (true) {
            $order_id_main = date('YmdHis') . rand(10000, 99999);//5位数
            $order_id_len = strlen($order_id_main);
            $order_id_sum = 0;
            for ($i = 0; $i < $order_id_len; $i++) {
                $order_id_sum += (int)(substr($order_id_main, $i, 1));
            }
            $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);
            return $order_id;

        }
    }

    //解密
    public static function decrypt($ciphertext){
//        $ciphertext = 'A93zdXJwVxQsOuPiA/2v9DmCtxBlddgEK1/UFJ69aeU=';
        $key = 'IFcJI3VsUkTqAQpq';
        $iv = 'DGyUIAKGuFPWuDX2';
        $decrypted = self::decrypt_AES($ciphertext, $key, $iv);
        return $decrypted;
    }

    public static function decrypt_AES($ciphertext, $key, $iv) {
        $cipher = "aes-128-cbc";
        $plaintext = openssl_decrypt(base64_decode($ciphertext), $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
        return $plaintext;
    }
}